package com.quyl.poi.service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class TestFuture {

    public static void main(String[] args) {

    }
    public static void main11(String[] args) throws ExecutionException, InterruptedException {
        Map<String,CompletableFuture<String>> map = new HashMap<>();
        long start = System.currentTimeMillis();
        Map<String,Object> mapres = new HashMap<>();
        CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> {
            try {
                System.out.println("执行开始1");
                Thread.sleep(5000);
                System.out.println("执行完成1");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return "success1";
        });
        map.put("future1",future1);
//        while (future1.isDone()) {mapres.put("future1",future1.get());}

        CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
            try {
                System.out.println("执行开始2");
                Thread.sleep(10000);
                System.out.println("执行完成2");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return "success2";
        });
        map.put("future2",future2);
//        while (future2.isDone()) {
//            mapres.put("future2",future2.get());
//        }
        CompletableFuture<String> future3 = CompletableFuture.supplyAsync(() -> {
            try {
                System.out.println("执行开始3");
                Thread.sleep(5000);
                System.out.println("执行完成3");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return "success3";
        });
        map.put("future3",future3);
//        while (future3.isDone()) {
//            mapres.put("future3",future3.get());
//        }
        for (Map.Entry<String, CompletableFuture<String>> entry : map.entrySet()) {
            while (!entry.getValue().isDone()) ;
            mapres.put(entry.getKey(),entry.getValue().get());
        }
        long end = System.currentTimeMillis();
        System.out.println(mapres);
        System.out.println("执行时间："+(end-start));
    }


}
